/*
RTC DS3231 simple driver for ATMEGA32
Copyright (C) 2015 Rodrigo Garcia 

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef RTCDS3231_H
#define RTCDS3231_H

#include "i2c.h"

///hardware
#define DS3231_SLAVE_ADDRESS 0x68

#define DS3231_WRITE 0
#define DS3231_READ 1

#define DS3231_ACK 0
#define DS3231_NACK 1

// register map DS3231
#define DS3231_SECS_REG 0x00
#define DS3231_MIN_REG 0x01
#define DS3231_HOUR_REG 0x02
#define DS3231_DOW_REG 0x03
#define DS3231_DOM_REG 0x04
#define DS3231_MON_REG 0x05
#define DS3231_YEAR_REG 0x06
// alarm registers (to implement)
/* DS3231 Register map */
/* 00h-06h: seconds, minutes, hours, day-of-week, date, month, year (all in BCD) */
/*  *       bit 7 should be set to zero: The DS3231 clock is always running */
/*  *  07h: A1M1  Alarm 1 seconds */
/*  *  08h: A1M2  Alarm 1 minutes */
/*  *  09h: A1M3  Alarm 1 hour (bit6 is am/pm flag in 12h mode) */
/*  *  0ah: A1M4  Alarm 1 day/date (bit6: 1 for day, 0 for date) */
/*  *  0bh: A2M2  Alarm 2 minutes */
/*  *  0ch: A2M3  Alarm 2 hour (bit6 is am/pm flag in 12h mode) */
/*  *  0dh: A2M4  Alarm 2 day/data (bit6: 1 for day, 0 for date) */
/*  *       <see data sheet page12 for Alarm register mask bit tables: */
/*  *        for alarm when hours, minutes and seconds match set 1000 for alarm 1> */
/*  *  0eh: control */
/*  *      bit7: !EOSC */
/*  *      bit6: BBSQW */
/*  *      bit5: CONV */
/*  *      bit4: RS2 */
/*  *      bit3: RS1 */
/*  *      bit2: INTCN */
/*  *      bit1: A2IE */
/*  *      bit0: A1IE */
/*  *  0fh: control/status */
/*  *      bit7: OSF */
/*  *      bit6: 0 */
/*  *      bit5: 0 */
/*  *      bit4: 0 */
/*  *      bit3: EN32kHz */
/*  *      bit2: BSY */
/*  *      bit1: A2F alarm 2 flag */
/*  *      bit0: A1F alarm 1 flag */
/*  * 10h: aging offset (signed) */
/*  * 11h: MSB of temp (signed) */
/*  * 12h: LSB of temp in bits 7 and 6 (0.25 degrees for each 00, 01, 10, 11) */

///software

struct RTC_time 
{
  char sec; // 0 to 59
  char min; // 0 to 59
  char hour; //0 to 59
  char mday; // 1 to 31
  char mon; // 1 to 12
  char year; // 0 to 99
  char wday; // 1 to 7

  unsigned char am;//  AM, PM 
  unsigned char twelveHour; // 12 hour clock time
};
// statically allocated struct for time value
//extern struct RTC_time _rtc_time;

uint8_t bcd2dec(uint8_t b);
uint8_t dec2bcd(uint8_t d);

// initializates the RTC in slow mode (100Khz)
void DS3231_init_slow(void);

// sets the time of the RTC
/* returns 0 if setting OK */
/* receives a RTC_time struct to write its values to RTC */
/* Note.- It converts from bcd2dec inside function */
/* Errors table: */
/*  1 = START condition */
/*  2 = SLA+W */
/*  3 = loading 0x00 register */
/*  4 = secs */
/*  5 = min */
/*  6 = hour */
/*  7 = wdays */
/*  8 = mdays */
/*  9 = mon */
/*  10 = year */
char DS3231_set_time(struct RTC_time time);

//gets the time reading all the DS3231 registers
/* 0 if reading OK */
/* receives a RTC_time struct pointer to modify the struct with data read from RTC */
/* Note.- Conversion from dec2bcd is done inside the function */
/* Errors table: */
/*  1 = START condition */
/*  2 = SLA+W */
/*  3 = loading  0x00 register */
/*  4 = repeated START */
/*  5 = SLA+R */
char DS3231_get_time(struct RTC_time* time);

#endif
